// import Cookies from 'js-cookie';
import axios from "axios";
import dateUtil from '@/utils/date.js'

// dateUtil.formatDate(
//   this.addParam.overdueTime1,
//   'yyyy-MM-dd hh:mm:ss'
// );

export default {
 
  setPoint: function (etJson) {
  	//main 所属应用  cmdata 公共数据  bussdata 业务数据
    const params = { main: 'emis-web', cmdata: {}, bussdata: {}}
    // 获取事件时间
    // var myDate = new Date()
    let userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}');
    let token = localStorage.getItem('access_token');
    params.bussdata.userId = userInfo.userId || token || etJson.userId; // 用户标识
    params.bussdata.username = etJson.username; // 用户名称
    params.bussdata.createTime = dateUtil.dateFormat(); // 时间戳
    params.bussdata.businessType = etJson.businessType; // 业务类型
    params.bussdata.opType = etJson.opType; // 操作类型
    params.bussdata.remoteUrl = etJson.remoteUrl; // 操作类型
    params.bussdata.data = etJson.data; // 操作类型
    params.bussdata.title = etJson.title; // 日志标题
    params.bussdata.method = etJson.method; // 请求方式

    // Document对象数据
    if (document) {
      params.cmdata.hostname = document.domain || ''
      params.cmdata.url = document.URL || ''
      params.cmdata.title = document.title || ''
      params.cmdata.referrer = document.referrer || ''
    }
    // //分辨率
    if (window && window.screen) {
      params.cmdata.sh = window.screen.height || 0
      params.cmdata.sw = window.screen.width || 0
      params.cmdata.cd = window.screen.colorDepth || 0
    }
    // //设备信息
    if (navigator) {
      params.cmdata.lang = navigator.language || ''
      params.cmdata.ua = navigator.userAgent || ''
      // 页面加载时间
      params.cmdata.loadT = window.performance.timing.domContentLoadedEventEnd - window.performance.timing.navigationStart || 0
    }

    const userId = '603'
    params.cmdata.userId = userId

    // 浏览器唯一标识
    // const uuid = Cookies.get('uuid')
    // if (uuid != null && uuid != '') {
    //   params.cmdata.sid = uuid
    // } else {
    //   const newUuid = this.generateUUID()
    //   Cookies.set('uuid', newUuid)
    //   params.cmdata.sid = newUuid
    // }
    // 拼接参数串
    var args = ''
    for (var i in params) {
      if (args != '') {
        args += '&'
      }
      args += i + '=' + encodeURIComponent(JSON.stringify(params[i]))
    }

    axios.post('/zuul/userCenter/customLog/save',params.bussdata).then((data)=>{
      if(data.code==='000000'){
        console.log('存储数据成功！');
      }
    }).catch(err=>{
      console.log(err);
    })

    // console.log(params);
    // 通过Image对象请求后端脚本
    // const img = new Image(1, 1)
    // img.src = 'http://192.168.152.102/log.gif?' + args


  },
  //生成唯一的UUID
  generateUUID() {
    let d = new Date().getTime();
    if (window.performance && typeof window.performance.now === "function") {
      d += performance.now(); //use high-precision timer if available
    }
    let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
      let r = (d + Math.random() * 16) % 16 | 0;
      d = Math.floor(d / 16);
      return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
    });
    return uuid;
  }
}
